/**
 * 格式化文件大小
 * @param bytes 字节数
 * @returns 格式化后的文件大小字符串
 */
export function formatFileSize(bytes: number): string {
    if (bytes === 0) return "0 B";

    const k = 1024;
    const sizes = ["B", "KB", "MB", "GB", "TB"];
    const i = Math.floor(Math.log(bytes) / Math.log(k));

    return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
}

/**
 * 获取文件图标类名
 * @param mimeType 文件MIME类型
 * @returns 对应的图标类名
 */
export function getFileIconClass(mimeType: string): string {
    if (mimeType.startsWith("image/")) {
        return "file-image";
    }
    if (mimeType.startsWith("video/")) {
        return "file-video";
    }
    if (mimeType.startsWith("audio/")) {
        return "file-audio";
    }
    if (mimeType === "application/pdf") {
        return "file-pdf";
    }
    if (mimeType.includes("spreadsheet") || mimeType === "text/csv") {
        return "file-excel";
    }
    if (mimeType.includes("wordprocessing")) {
        return "file-word";
    }
    if (mimeType.includes("presentation")) {
        return "file-powerpoint";
    }
    if (mimeType.startsWith("text/")) {
        return "file-text";
    }
    return "file";
}

/**
 * 获取文件类型显示文本
 * @param mimeType 文件MIME类型
 * @returns 文件类型显示文本
 */
export function getFileTypeText(mimeType: string): string {
    const types: Record<string, string> = {
        "image/jpeg": "JPEG图片",
        "image/png": "PNG图片",
        "image/gif": "GIF图片",
        "application/pdf": "PDF文档",
        "text/plain": "文本文件",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
            "Word文档",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
            "Excel表格",
        "text/csv": "CSV文件",
        "application/vnd.openxmlformats-officedocument.presentationml.presentation":
            "PPT演示文稿",
    };

    return types[mimeType] || "未知文件类型";
}

export function isImageFile(mimeType: string): boolean {
    return mimeType.startsWith("image/");
}
